<?php
/**
 * Firal CMS
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.opensource.org/licenses/bsd-license.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to firal-dev@googlegroups.com so we can send you a copy immediately.
 *
 * @category   Firal
 * @package    Firal_Modules
 * @subpackage Default
 * @copyright  Copyright (c) 2008 MaakSite.nl Productions. (http://productions.maaksite.nl/)
 * @license    http://www.opensource.org/licenses/bsd-license.php     New BSD License
 * @version    $Id: FiralPagesController.php 11 2008-06-30 16:14:44Z pieterkokx $
 */

/**
 * @see Zend_Controller_Action
 */
require_once 'Zend/Controller/Action.php';

/**
 * Default PagesController
 *
 * @category   Firal
 * @package    Firal_Modules
 * @subpackage Default
 * @copyright  Copyright (c) 2008 MaakSite.nl Productions. (http://productions.maaksite.nl/)
 * @license    http://www.opensource.org/licenses/bsd-license.php     New BSD License
 */
class Admin_FiralPagesController extends Zend_Controller_Action
{
    
    /**
     * Show the pages
     * 
     * @return void
     */
    public function indexAction()
    {
        $cache = Zend_Registry::get('firal_cache');
        /* @var $cache Zend_Cache_Core */
        
        if (!($pages = $cache->load('pages_pages'))) {
            // get the pages list
            $pages = Doctrine_Query::create()
                                   ->select('COUNT(r.id), p.id, p.title, p.allow_replies, u.id, u.username')
                                   ->from('FiralPage p')
                                   ->leftJoin('p.Reply r')
                                   ->innerJoin('p.User u')
                                   // postgreSQL likes a lot of GROUP BY columns
                                   ->groupBy('p.id, p.title, p.allow_replies, u.id, u.username')
                                   ->execute(array(), Doctrine::HYDRATE_ARRAY);
            $cache->save($pages, 'pages_pages', array('default_pages'));
        }
        
        $this->view->pages = $pages;
    }
    
    /**
     * Edit a page
     * 
     * @return void
     */
    public function editAction()
    {
        $id = $this->getRequest()->getParam('id');
    	
    	if (!is_numeric($id)) {
    	    // an error exception later
    	    return;
    	}
    	
    	$id = (int) $id;
    	
    	$cache = Zend_Registry::get('firal_cache');
        /* @var $cache Zend_Cache_Core */
    	
    	if (!($page = $cache->load('pages_singlepage_edit_' . $id))) {
        	$page = Doctrine_Query::create()
        	                     ->select('p.id, p.title, p.content, p.allow_replies, u.username, u.id')
        	                     ->from('FiralPage p')
        	                     ->innerJoin('p.User u')
        	                     ->where('p.id=?', $id)
        	                     ->execute();
        	
        	$cache->save($page, 'pages_singlepage_edit_' . $id, array('default_pages'));
    	}
    	
    	$this->view->page = $page;
    }
}